package com.tt.controller;

import com.tt.annotation.NoRepeatSubmit;
import com.tt.bto.RegionParam;
import com.tt.common.Const.TtConstans;
import com.tt.common.exception.TtException;
import com.tt.common.page.TtResult;
import com.tt.pojo.system.Region;
import com.tt.pojo.system.RegionTreeVo;
import com.tt.service.IRegionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 青青
 */
@Controller
@RequestMapping("/region")
public class RegionController
{
    private String prefix = "system";

    @Autowired
    private IRegionService regionServiceImpl;

    @GetMapping("/region")
    public String region(){return prefix + "/region";}

    /**
     * 获取
     * @return
     * @throws Exception
     */
    @PostMapping("/getRegionList")
    public @ResponseBody List<RegionTreeVo> getRegionList(@RequestParam("pid") Integer pid,
                                                          @RequestParam(value = "id",required = false) Integer id
                                                          ) throws Exception
    {
        if (!StringUtils.isEmpty(id))
        {
            return regionServiceImpl.getRegionList(id);
        }
        return regionServiceImpl.getRegionList(pid);
    }

    /**
     * 更新
     * @param region
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/updateRegion")
    public TtResult updateRegion(Region region) throws Exception
    {
        return TtResult.ok(regionServiceImpl.updateRegion(region));
    }

    /**
     * 根据Id下的叶子节点
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/delRegion")
    public TtResult delRegion(Integer id) throws Exception
    {
        return TtResult.ok(regionServiceImpl.delRegion(id));
    }



    @NoRepeatSubmit
    @PostMapping("/addBatchRegion")
    @ResponseBody
    public TtResult addBatchRegion(RegionParam regionParam) throws Exception
    {

        if (StringUtils.isEmpty(regionParam.getContext()))
        {
            if(StringUtils.isEmpty(regionParam.getName())){
                throw new TtException("区域名称必填");
            }

            Region region = Region.builder().build();
            BeanUtils.copyProperties(regionParam,region);
            region.setCharin(regionParam.getCharin()+TtConstans.Connect_x+regionParam.getPId());
            region.setLevel(regionParam.getLevel()+1);
            int i = regionServiceImpl.addRegion(region);
            return TtResult.ok(i);
        }
        List<Region> regions = buildRegion(regionParam);
        int addRegion = regionServiceImpl.addBatchRegion(regions);
        return TtResult.ok(addRegion);
    }

    /**
     * 构建插入条件
     * @param regionParam
     * @return
     */
    private List<Region> buildRegion(RegionParam regionParam)
    {
        String context = regionParam.getContext();
        Integer pId = regionParam.getPId();
        String parentCode = regionParam.getParentCode();
        String[] twigs = context.split("\n");
        List<Region> list = new ArrayList<>();
        if (twigs.length >0 )
        {
            for (int i = 0; i < twigs.length; i++)
            {
                String twig = twigs[i];

                Region r = Region.builder().build();
                String[] strip = twig.split("\t");
                String code = strip[0];
                String name = strip[1];
                if (strip.length > 2){
                    name = strip[2];
                }

                r.setCode(code);
                r.setName(name);
                r.setParentCode(parentCode);
                r.setPId(pId);
                r.setLevel(regionParam.getLevel()+1);
                r.setCharin(regionParam.getCharin()+TtConstans.Connect_x+regionParam.getPId());
                list.add(r);
            }
        }
        List<Region> regionByPid = regionServiceImpl.getRegionByPid(pId);
        if (!CollectionUtils.isEmpty(regionByPid))
        {
            for (Region r: regionByPid)
            {
                String name = r.getName();

                for (Region r2: list)
                {
                    String r2Name = r2.getName();
                    if (name.equals(r2Name))
                    {
                        throw new TtException("存在重复元素："+r2Name);
                    }
                }

            }
        }

        return list;
    }





}
